﻿global using Devonline.AspNetCore;
global using Devonline.Core;
global using Devonline.Entity;
global using Devonline.Identity;
global using Devonline.Identity.Admin.Models;
global using static Devonline.Core.AppSettings;
using Devonline.AuxiliaryTools.AITools;
using Devonline.CloudService.Feige;
using Devonline.CloudService.Tencent;
using Devonline.Communication.Messages;
using Devonline.Identity.Admin;
using Devonline.Logging;
using OfficeOpenXml;

#if DEBUG
ThreadPool.SetMinThreads(UNIT_HUNDRED, UNIT_HUNDRED);
#endif

var builder = WebApplication.CreateBuilder(args);
var appSetting = builder.AddSetting<AdminSetting>();
ExcelPackage.License.SetNonCommercialOrganization(appSetting.Copyright ?? appSetting.Name);

builder.AddServiceDefaults();
builder.Host.AddLogging();
builder.WebHost.ConfigureKestrel(appSetting);
builder.AddCommunicator();
var services = builder.Services;

services.AddDefaultServices(appSetting);
services.AddIdentityDataService();
services.AddDefaultIdentity(appSetting);
services.AddTransient<AuthorizationService>();
services.AddTransient<AuthenticationService>();
services.AddSmsCaptchaService(appSetting.Sms, appSetting.SmsCaptcha);
services.AddWeixinOpenPlatformService(appSetting.WeixinApp);
services.AddWeixinMiniProgramService(appSetting.MiniProgram);
services.AddTransient<PhoneNumberCaptchaService>();
services.AddTransient<WeixinIdentityService>();
services.AddFramesCaptureService();

var mvcBuilder = services.AddControllersWithViews(appSetting);

#if DEBUG
mvcBuilder.AddRazorRuntimeCompilation();
#endif

services.AddDatabase(appSetting);
services.AddDefaultAuthentication(appSetting);
services.AddDefaultAuthorization(appSetting);
services.AddMigrationIdentityDatabase(appSetting);

var app = builder.Build();
app.UseDefaultBuilder(appSetting);
await app.Services.GetRequiredService<IMessageCommunicator>().StartAsync();
app.Run();